// pages/livePlayer/liveData/index.js
let config = require("../../../utils/config");
import { http } from "../../../utils/request";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    po: "",
    liveData: null,
    personList: [],
    pageIndex: 1,
    isLoad: false,
    isClick: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      po: options.po
    })
    this.queryLiveData();
  },

  /** 刷新 */
  refreshTap(){
    this.setData({
      pageIndex: 1,
      personList: []
    })
    this.queryLiveData();
  },

  /** 直播数据 */
  queryLiveData() {
    let that = this;
    http.requestData("POST", "LiveRoom/GetLiveRoomMember", {
      po: this.data.po,
      pageIndex: this.data.pageIndex,
      pageSize: 10
    }, true).then(res => {
      wx.stopPullDownRefresh();
      if (res.Tag == "10") {
        if (that.data.pageIndex == 1) {
          that.data.personList = [];
        }
        res.Data.LiveRoomMember.Data.forEach(item => {
          item.Portrait = item.Portrait ? item.Portrait : "/images/ic-default-head.png";
          item.AnswerResult = !item.AnswerResult ? "未答题" : (item.AnswerResult == 20 ? "答题正确" : "答题错误")
          if (res.Data.LiveRoomInfo.ActiveType == 10) {//红包
            if(item.Red){
              item.prize = "+" + item.Red + "元";
              // RedStatus： null-未打款，10-已打款，20-打款中， 30-打款失败
              if(item.RedStatus){
                item.RedStatusTxt = item.RedStatus==10 ? "(已打款)" : (item.RedStatus==20?"(打款中)": "(打款失败)");  //失败：30
              }else{
                item.RedStatusTxt = "未打款";
              }
            }else{
              item.prize = item.IsClickActive ? "谢谢参与" : "未领取";
              item.RedStatusTxt = "";
            }
          } else if (res.Data.LiveRoomInfo.ActiveType == 20) {//积分
            item.prize = item.IntegralNum ? "+" + item.IntegralNum + "分" : "";
            item.RedStatusTxt = "";
          } else {//未设置活动
            item.prize = "";
            item.RedStatusTxt = "";
          }
          that.data.personList.push(item);
        })
        that.setData({
          isLoad: res.Data.LiveRoomMember.Data.length < 10 ? true : false,
          liveData: res.Data.LiveRoomInfo,
          personList: that.data.personList
        })
      } else {
        http.showToast(res.Message)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  // 数据导出
  exportDataTap() {
    http.requestData("POST", "LiveRoom/ExportVisitorDetailsData", {
      PO: this.data.po
    }, true).then(res => {
      if (res.Tag == 10) {
        wx.showLoading();
        //下载文件
        wx.downloadFile({
          url: config.hostUrl + "/" + res.Data,
          success(res) {
            // console.log("本地临时路径：",res.tempFilePath)
            //打开保存问价
            wx.openDocument({
              filePath: res.tempFilePath,
              showMenu: true,
              complete(e) {
                wx.hideLoading();
              }
            })
            return;
            wx.getFileSystemManager().saveFile({
              tempFilePath: res.tempFilePath,
              success(_res) {
                http.showToast("导出成功");
                console.log("保存成功：", _res.savedFilePath)
              },
              fail(err) {
                console.log("保存失败")
              }
            })
          },
          fail(err) {
            wx.hideLoading();
            console.log("下载失败")
          }
        })
      } else {
        http.showToast(res.Message);
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /** 补发奖励 */
  reissueTap(e) {
    let i = e.currentTarget.dataset.i
    let person = this.data.personList[i];
    if (this.data.isClick) {
      return;
    }
    this.setData({
      isClick: true
    })
    wx.showLoading({
      title: '补发中...',
    })
    let that = this;
    http.requestData("POST", "LiveRoom/RepaySingle", {
      Po: person.PO, //房间号
      ManagerId: person.ManageId, //店长id
      MemberId: person.MemberId, //用户id
      IsNoSaveExam: person.AnswerResult ? true : false //是否答题
    }).then(res => {
      wx.hideLoading();
      that.setData({
        isClick: false
      })
      if (res && res.Tag == 10) {
        http.showToast("补发成功");
        that.data.personList[i].RedStatus = 10;
        that.setData({
          personList: that.data.personList
        })
      } else {
        http.showToast(res.Message);
      }
    }).catch(err => {
      wx.hideLoading();
      that.setData({
        isClick: false
      })
    })
  },

  /** 拉黑 */
  blockTap(e) {
    let that = this;
    let i = e.currentTarget.dataset.i
    let person = this.data.personList[i];
    wx.showModal({
      title: '提示',
      content: '确定拉黑该用户吗？',
      complete: (res) => {
        if (res.confirm) {
          that.blockConfirm(person);
        }
        if (res.cancel) {
          //todo
        }
      }
    })
  },
  /**确定拉黑 */
  blockConfirm(person) {
    http.requestData("POST", "MemberBlack/Save", {
      "InviteCode": person.ManageId,//店长Id
      "Mobile": config.TEL(), //店长手机号
      "MemberId": person.MemberId, //拉黑用户的小程序标识
      "MemberMobile": person.MemberMobile, //客户手机号
      "Nickname": person.MemberName,
      "Portrait": person.Portrait
    }).then(res => {
      if (res && res.Tag == 10) {
        http.showToast("拉黑成功");
      } else {
        http.showToast(res.Message);
      }
    }).catch(err => {
      http.showToast("网络服务异常")
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      pageIndex: 1
    })
    this.queryLiveData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoad) {
      return
    }
    this.setData({
      pageIndex: this.data.pageIndex + 1
    })
    this.queryLiveData();
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {}
})